#include "ui/mainWidget/mainWidget.h"

mainWidget::mainWidget(QApplication* a, Language tsor, ShowMode showMode, QWidget *parent)
    : QWidget(parent), m_A(a)
{
    m_Menu = new menuWidget();
    m_Option = new optionWidget();
    m_Option->optionWidth(ParameterKind::Set, 330);     // 设置选项视窗的宽度
    m_Video = new videoWidget();                        // 初始化摄像头模组
    m_MainHLayout = new QHBoxLayout();
    m_SettingDialog = new SettingDialog();
    m_SettingDialog->setWindowFlags(m_SettingDialog->windowFlags() | Qt::WindowStaysOnTopHint);
    m_SettingDialog->setWindowModality(Qt::ApplicationModal);  // 设置为应用程序模态

    m_Tsor = new QTranslator();
    m_MainHLayout->setMargin(0);                        // 设置不同布局之间的间隙
    m_MainHLayout->addWidget(m_Menu);
    m_MainHLayout->addWidget(m_Option);
    m_MainHLayout->addWidget(m_Video);
    m_MainHLayout->setSpacing(0);                       // 设置该布局中不同控件之间的间隙
    this->setLayout(m_MainHLayout);

    connect(m_Menu, SIGNAL(menuBtnClicked(int)), this, SLOT(menuWidgetClickedBtn(int)));
    //connect(option, SIGNAL(cameraChange(const QString&)), video, SLOT(slotsChangeCamera(const QString&)));   // 绑定option相机中心部件对应的切换相机信号
    connect(m_Option, &optionWidget::cameraChange, m_Video, &videoWidget::slotChangeCamera);
    //connect(option, SIGNAL(flushCamera()), video, SLOT(flushCamera()));
    menuWidgetClickedBtn(-1);                                                           // 因为构造函数不能发送信号，所以menuWidgetClickedBtn槽函数开始不可以通过menuWidget类发送的信号触发，而通过当前这种比较笨拙的方式触发
    connect(m_Option, &optionWidget::setSignalValue, this, &mainWidget::setValue);      // 绑定optionWidget发送的修改曝光时长的信号
    connect(m_SettingDialog, &SettingDialog::senderLanguage, this, &mainWidget::setLanguage);
    connect(m_SettingDialog, &SettingDialog::senderShowMode, this, &mainWidget::setShowModel);          // 绑定对话框修改显示模式信号
    connect(m_SettingDialog, &SettingDialog::rejected, this, [=](){
        m_Menu->SettingBtnClose();
        DLOG("设置按钮关闭成功");
    });
    // 设置从xml配置文件获取到的语言版本与显示模式
    this->setShowModel(showMode);
    this->setLanguage(tsor);
}

mainWidget::~mainWidget()
{
}

bool mainWidget::setShowModel(ShowMode showMode)
{
   if(showMode == ShowMode::DAY)        // 如果是白天模式
   {
       m_Option->setBackgroundColor(QColor(Qt::white), QColor(Qt::black));      // 设置第二列OptionWidget的背景/字体颜色
       {                                // 如果是第一列的图标区域还需要将对应按钮颜色设置为对应配套颜色（黑夜模式->红色/白天模式->普通）// 目前还没有找适配的icon
           m_Menu->setBackgroundColorFormPalette(QColor(Qt::white));
       }
        m_Video->setBackGroundColor(QColor(Qt::white));
   }else{                               // 如果是黑夜模式
       m_Option->setBackgroundColor(QColor(Qt::black), QColor(Qt::red));
       {
           m_Menu->setBackgroundColorFormPalette(QColor(Qt::black));
       }
       m_Video->setBackGroundColor(QColor(Qt::black));
   }
   return true;
}

void mainWidget::setLanguage1(Language lage)
{
    //DLOG("进入翻译修改");
    m_Option->optionLanguageInit();
    if(!m_Tsor->isEmpty())                      // 检查之前是否已经安装翻译器
    {
        m_A->removeTranslator(m_Tsor);          // 卸载之前安装的翻译器
    }
    switch(lage)
    {
    case Language::Chinese:
        break;
    case Language::English:
        DLOG("语言修改为英文");
        m_Tsor->load(QCoreApplication::applicationDirPath() + "/../Language/English.qm");
        m_A->installTranslator(m_Tsor);
        DLOG("翻译器安装成功");
        break;
    }
}

void mainWidget::setLanguage(Language lage)
{
    setLanguage1(lage);
    setLanguage1(lage);
}

void mainWidget::menuWidgetClickedBtn(int btn)
{
    // 判断用户点击的是否是设置按钮
    if(btn == optionKind::SettingKind)
    {
        m_SettingDialog->show();
    }else
    {
        // 调用optionWidget中的方法将当前点击的菜单按钮作为参数传递
        m_Option->setItemShow((optionKind)btn);
    }
}

void mainWidget::setValue(signalKind kind, int value)
{
    m_Video->setCameraValue(kind, value);
}

